﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace DAO
{
    public class DoanhThuDAO
    {
        public DataTable LoadDoanhThu()
        {
            Data a = new Data();
            DataTable table = new DataTable();
            string sql = "Select* from DoanhThu order by Ngay DESC";
            table = a.ExecuteQuery(sql);
            return table;
        }
        public DataTable LoadDoanhThuTop(int _numb)
        {
            Data a = new Data();
            DataTable table = new DataTable();
            string sql = "";
            if (_numb != 0)
            {
                sql += "select top ";
                sql += _numb + "* ";
            }
            else
                sql += "select * ";            
            sql += "from DoanhThu order by Ngay DESC, ";
            sql += "case ";
            sql += "when Buoi='Tối' then 0 ";
            sql += "when Buoi='Sáng' then 1 ";
            sql += "end ";
            table = a.ExecuteQuery(sql);
            return table;
        }
        public void UpdateSoHoaDon(int _maDT, string _soHD)
        {
            Data a = new Data();
            string sql = "update DoanhThu ";
            sql += "set SoHoaDon='" + _soHD + "' ";
            sql += "where MaDT=" + _maDT;
            a.ExecuteNonQuery(sql);
        }
        public void UpdateGiamGia_ConLai(int _maDT, float _giamGia, float _conLai)
        {
            Data a = new Data();
            string sql = "update DoanhThu ";
            sql += "set GiamGia=" + _giamGia + " ";
            sql += "where MaDT=" + _maDT;
            a.ExecuteNonQuery(sql);
            sql = "update DoanhThu ";
            sql += "set ConLai=" + _conLai + " ";
            sql += "where MaDT=" + _maDT;
            a.ExecuteNonQuery(sql);
        }
        public void NapDuLieuMoi()
        {
            string cnStr = ConfigurationManager.ConnectionStrings["KetNoi"].ConnectionString;
            SqlConnection con = new SqlConnection(cnStr);
            con.Open();
            string sql = "usp_DoanhThuMoi";
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            con.Close();
        }
        public DataTable LoadHoaDonTheoMaDT(int _maDT)
        {
            string cnStr = ConfigurationManager.ConnectionStrings["KetNoi"].ConnectionString;
            SqlConnection con = new SqlConnection(cnStr);
            con.Open();
            string sql = "usp_LoadHoaDonTheoMaDT";
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = sql;
            cmd.Parameters.Add("@MaDT", SqlDbType.Int);
            cmd.Parameters["@MaDT"].Value = _maDT;
            //cmd.ExecuteNonQuery();
            DataTable table = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(table);
            con.Close();
            return table;
        }

    }
}